package cmd

import (
	"context"
	"log"

	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/net/ghttp"
	"github.com/gogf/gf/v2/os/gcmd"
	"github.com/gogf/gf/v2/util/gmode"

	"web-ppt/internal/controller/login"
	"web-ppt/internal/controller/ppt"
	"web-ppt/internal/logic/middleware"
)

var (
	Main = gcmd.Command{
		Name:  "web-ppt",
		Usage: "web-ppt",
		Brief: "start web-ppt server",
		Func: func(ctx context.Context, parser *gcmd.Parser) (err error) {
			s := g.Server()

			// HOOK, 开发阶段禁止浏览器缓存,方便调试
			if gmode.IsDevelop() {
				s.BindHookHandler("/*", ghttp.HookBeforeServe, func(r *ghttp.Request) {
					r.Response.Header().Set("Cache-Control", "no-store")
				})
			}

			s.Group("/", func(group *ghttp.RouterGroup) {
				// group.Middleware(ghttp.MiddlewareHandlerResponse)
				group.Middleware(middleware.ResponseHandler)
				group.Group("/", func(group *ghttp.RouterGroup) {
					group.Bind(
						ppt.NewV1().Index,
						ppt.NewV1().Show,
						ppt.NewV1().ContentList,
						login.NewV1(),
					)
				})

				// 权限控制路由
				group.Group("/", func(group *ghttp.RouterGroup) {
					group.Middleware(middleware.Auth)
					group.Bind(
						ppt.NewV1().ContentAddAndUpdate,
						ppt.NewV1().ContentDel,
						ppt.NewV1().Del,
						ppt.NewV1().AddAndUpdate,
						ppt.NewV1().DianjiIndex,
						ppt.NewV1().DelDianji,
						ppt.NewV1().SaveDianji,
					)
				})
			})
			log.Println("访问地址为: http://127.0.0.1:8236")
			s.Run()
			return nil
		},
	}
)
